package com.fjnu.controller.common;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fjnu.dao.common.DaoException;


public class BaseController {
	Logger log = LogManager.getLogger(BaseController.class.getName());

	public static final String SUCCESS = "success";
	public static final String ERROR = "error";
	public static final String FAIL = "fail";
	public static final String CAUSE = "cause";
	public static final String STATUS = "status";

	Map<String, Object> data;

	public Map<String, Object> init(Object... params) {
		//String path = getPreMethod();
		StringBuffer paramBuffer = new StringBuffer();
		for (Object object : params) {
			paramBuffer.append(object);
			paramBuffer.append(", ");
		}
		log.debug(paramBuffer.toString());

		data = new HashMap<String, Object>();
		for (int i = 0, length = params.length; i < length; i++) {
			Object param = params[i];
			if (param == null) {
				data.put(STATUS, ERROR);
				data.put(CAUSE, "第" + (i + 1) + "个参数不能为空");
				return data;
			}
		}
		data.put(STATUS, SUCCESS);
		return data;
	}
	public Map<String, Object> getData2Result() {
		HttpServletRequest request = 
				((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		HttpServletResponse response=
				((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		String jsonpCallBack = request.getParameter("callBackParamName");
		if (jsonpCallBack != null) {
			request.setAttribute("isJSONP", true);
			try {
				Writer writer = new OutputStreamWriter(response.getOutputStream());
				writer.write(jsonpCallBack + "(");
				writer.flush();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		return data;
	}

	public void put(String key, Object value) {
		getData().put(key, value);
	}

	public Map<String, Object> putFail(Object value) {
		put(STATUS, FAIL);
		put(CAUSE, value);
		return getData();
	}

	public Map<String, Object> putError(Object value) {
		put(STATUS, ERROR);
		put(CAUSE, value);
		return getData();
	}

	/* 基于@ExceptionHandler异常处理 */
	@ExceptionHandler
	@ResponseBody
	public Map<String, Object> exp(HttpServletRequest request, Exception ex) {
		// 根据不同错误转向不同页面
		if (ex instanceof DaoException) {
			putFail(ex.getMessage());
		}
		else if (ex instanceof Exception) {
			//System.out.println("是我错了");
			putError(ex.getMessage());
		}
		return getData();
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

}
